package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.DstTxnAuthCfgMapper;
import cc.rengu.oltp.service.common.entity.DstTxnAuthCfg;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 交易配置表操作方法实现类
 * Created by 王成 on 2017/11/22.
 */
public class DstTxnAuthCfgMapperImpl implements DstTxnAuthCfgMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public DstTxnAuthCfgMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public void loadAllDstTxnAuthCfg(Set<String> dbPoolCashNode, List<DstTxnAuthCfg> dstTxnAuthCfgList) throws Exception {
        for (String dbCaseNodeName : dbPoolCashNode) {
            String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbCaseNodeName, AppParamConstant.OLTP_POOL_NAME);
            DbsUtil dbsUtil = new DbsUtil(dbPoolName);
            String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "loadAllDstTxnAuthCfg");
            List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, null, DstTxnAuthCfg.class);
            if (objectList.isEmpty()) {
                rglog.warn("获取数据库连接池【{}】全量交易配置信息失败!", dbPoolName);
            } else {
                dstTxnAuthCfgList.addAll(objectList.stream().map(item -> (DstTxnAuthCfg) (item)).collect(Collectors.toList()));
            }
        }
    }

    @Override
    public void loadAllDstTxnAuthCfg(List<DstTxnAuthCfg> dstTxnAuthCfgList) throws Exception {
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "loadAllDstTxnAuthCfg");
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, null, DstTxnAuthCfg.class);
        if (objectList.isEmpty()) {
            rglog.warn("获取数据库连接池【{}】全量交易配置信息失败!", dbPoolName);
        } else {
            dstTxnAuthCfgList.addAll(objectList.stream().map(item -> (DstTxnAuthCfg) (item)).collect(Collectors.toList()));
        }
    }

    @Override
    public List<DstTxnAuthCfg> selectDstTxnAuthCfgInfo(String instId, String msgDstId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectDstTxnAuthCfgInfo");
        DstTxnAuthCfg dstTxnAuthCfg = new DstTxnAuthCfg();
        dstTxnAuthCfg.setInstId(instId);
        if (null != msgDstId && !msgDstId.isEmpty()) {
            dstTxnAuthCfg.setMsgDstId(msgDstId);
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, dstTxnAuthCfg, DstTxnAuthCfg.class);
        if (objectList.isEmpty()) {
            rglog.warn("根据法人行标识及通道标识获取所有通道权限配置信息失败，instId:<{}>,msgDstId:<{}>", instId, msgDstId);
            return null;
        }
        return objectList.stream().map(item -> (DstTxnAuthCfg) (item)).collect(Collectors.toList());
    }

    @Override
    public DstTxnAuthCfg selectDstTxnAuthCfgByPrimaryKey(String instId, String msgDstId, String channelTxn) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectDstTxnAuthCfgByPrimaryKey");
        DstTxnAuthCfg dstTxnAuthCfg = new DstTxnAuthCfg();
        dstTxnAuthCfg.setInstId(instId);
        dstTxnAuthCfg.setMsgDstId(msgDstId);
        dstTxnAuthCfg.setChannelTxn(channelTxn);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, dstTxnAuthCfg, dstTxnAuthCfg);
        if (0 != iReturnCode) {
            rglog.warn("根据主键查询通道交易权限配置信息失败,instId:<{}>,msgDstId:<{}>,channelTxn:<{}>,iReturnCode:<{}>", instId, msgDstId, channelTxn, iReturnCode);
            return null;
        }
        return dstTxnAuthCfg;
    }

    @Override
    public int updateDstTxnAuthCfgByPrimaryKey(DstTxnAuthCfg dstTxnAuthCfg) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateDstTxnAuthCfgByPrimaryKey");
        dstTxnAuthCfg.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, dstTxnAuthCfg, null);
        if (0 != iReturnCode) {
            rglog.error("根据主键更新通道交易权限配置信息失败,instId:<{}>,msgDstId:<{}>,channelTxn:<{}>,iReturnCode:<{}>", dstTxnAuthCfg.getInstId(), dstTxnAuthCfg.getMsgDstId(), dstTxnAuthCfg.getChannelTxn(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int insertDstTxnAuthCfg(DstTxnAuthCfg dstTxnAuthCfg) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertDstTxnAuthCfg");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, dstTxnAuthCfg, null);
        if (0 != iReturnCode) {
            rglog.error("登记通道交易权限配置信息失败,instId:<{}>,msgDstId:<{}>,channelTxn:<{}>,iReturnCode:<{}>", dstTxnAuthCfg.getInstId(), dstTxnAuthCfg.getMsgDstId(), dstTxnAuthCfg.getChannelTxn(), iReturnCode);
        }
        return iReturnCode;
    }
}
